from flask_wtf import FlaskForm #导入构建form类的基类
import wtforms #导入构建form类的元素类
from wtforms import validators

from wtforms.validators import ValidationError
class KeyWords:
    def __init__(self,message): #init当中用来接收报错的提示语
        self.message = message
        self.pull = ["admin"]

    def __call__(self, form, field): #像函数一样被调用
        keywords = field.data #获取form表单提交的数据
        if keywords in self.pull: #进行逻辑判断
            raise ValidationError(self.message) #弹出错误

def len9(form, field):
    keywords = field.data
    if len(keywords) == 9:
        raise ValidationError("用户名长度不可以为9")  # 弹出错误


class LoginForm(FlaskForm): #继承flaskForm定义自己的表单类
    username = wtforms.StringField(
        validators = [
            validators.Length(
            min = 3,
            max = 8,
            message = "用户名的长度必须是6到8位"
            ),
            KeyWords("用户名不可以是关键字"),
            len9
        ]
    ) #定义表单的元素
    password = wtforms.StringField()